﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            long id = Convert.ToInt64(Request.QueryString["id"]);
        }
    }

    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        
            string name = TbUser.Text;
            string password = TbPassword.Text;
            if (!IsExist(name))
            {
                Debug.WriteLine("用户名不存在!");
            }
            if(password != userDao.Find(name).Password)
            {
                Debug.WriteLine("密码不正确!");

            }
            else
            {
                Debug.WriteLine("登录成功!");
            }
      
    }
    protected void BtnRegister_Click(object sender, EventArgs e)
    {
        string name = TbUser.Text;
        if (!IsExist(name))
        {
            SaveInfo();
        }
       


    }

    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        string name = TbUser.Text;
        update(name);
    }

    protected void BtnDelete_Click(object sender, EventArgs e)
    {
        string name = TbUser.Text;
        delete(name);
    }

    private void delete(string name)
    {
        userDao.Delete(name);
    }
    private bool IsExist(string name)
    {
        return userDao.IsExist(name);
    }
    private void update(string name)
    {

        User data = userDao.Find(name);
        TbUser.Text = data.AdminName;
        TbPassword.Text = data.Password;
        HfId.Value = data.Id.ToString();
        SaveInfo();
    }

    private void SaveInfo()
    {
        string name = TbUser.Text.Trim();
        User data = userDao.Find(name);
        data.AdminName = TbUser.Text.Trim();
        data.Password = TbPassword.Text.Trim();
        userDao.Update(data);

    }

    private void AddInfo()
    {
        User data = new User();
        data.AdminName = TbUser.Text.Trim();
        data.Password = TbPassword.Text.Trim();
        long insertId = userDao.Insert(data);
        Debug.WriteLine("插入数据返回Id:" + insertId);
    }
}